package com.my.study.service.impl;

import com.my.study.dao.SysRoleDao;
import com.my.study.entity.SysRoleEntity;
import com.my.study.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 事务回滚： https://www.itqiankun.com/article/transaction-failure
 *  https://segmentfault.com/a/1190000013341344#:~:text=%20%E4%BA%8B%E5%8A%A1%E4%BC%A0%E6%92%AD%E8%A1%8C%E4%B8%BA%E6%98%AFSpring%E6%A1%86%E6%9E%B6%E7%8B%AC%E6%9C%89%E7%9A%84%E4%BA%8B%E5%8A%A1%E5%A2%9E%E5%BC%BA%E7%89%B9%E6%80%A7%EF%BC%8C%E4%BB%96%E4%B8%8D%E5%B1%9E%E4%BA%8E%E7%9A%84%E4%BA%8B%E5%8A%A1%E5%AE%9E%E9%99%85%E6%8F%90%E4%BE%9B%E6%96%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E8%A1%8C%E4%B8%BA%E3%80%82,%E8%BF%99%E6%98%AFSpring%E4%B8%BA%E6%88%91%E4%BB%AC%E6%8F%90%E4%BE%9B%E7%9A%84%E5%BC%BA%E5%A4%A7%E7%9A%84%E5%B7%A5%E5%85%B7%E7%AE%B1%EF%BC%8C%E4%BD%BF%E7%94%A8%E4%BA%8B%E5%8A%A1%E4%BC%A0%E6%92%AD%E8%A1%8C%E5%8F%AF%E4%BB%A5%E4%B8%BA%E6%88%91%E4%BB%AC%E7%9A%84%E5%BC%80%E5%8F%91%E5%B7%A5%E4%BD%9C%E6%8F%90%E4%BE%9B%E8%AE%B8%E5%A4%9A%E4%BE%BF%E5%88%A9%E3%80%82%20%E4%BD%86%E6%98%AF%E4%BA%BA%E4%BB%AC%E5%AF%B9%E4%BB%96%E7%9A%84%E8%AF%AF%E8%A7%A3%E4%B9%9F%E9%A2%87%E5%A4%9A%EF%BC%8C%E4%BD%A0%E4%B8%80%E5%AE%9A%E4%B9%9F%E5%90%AC%E8%BF%87%E2%80%9Cservice%E6%96%B9%E6%B3%95%E4%BA%8B%E5%8A%A1%E6%9C%80%E5%A5%BD%E4%B8%8D%E8%A6%81%E5%B5%8C%E5%A5%97%E2%80%9D%E7%9A%84%E4%BC%A0%E8%A8%80%E3%80%82%20%E8%A6%81%E6%83%B3%E6%AD%A3%E7%A1%AE%E7%9A%84%E4%BD%BF%E7%94%A8%E5%B7%A5%E5%85%B7%E9%A6%96%E5%85%88%E9%9C%80%E8%A6%81%E4%BA%86%E8%A7%A3%E5%B7%A5%E5%85%B7%E3%80%82
 * 失效原因一： 同一个类中，没有事务的方法A()，调用了有事务的方法B()
 *
 *
 *
 * @author: yidujun
 * @create: 2021/04/29 10:04
 */
@Slf4j
@Service("sysRoleService")
public class SysRoleServiceImpl implements SysRoleService {

    @Resource
    SysRoleDao sysRoleDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String get(){
        SysRoleEntity entity = new SysRoleEntity();
        entity.setId(2L);
        entity.setDescription("管理员");
        int update = sysRoleDao.update(entity);
        log.info("update: [{}]", update);

        this.testInner();
//        SysRoleServiceImpl bean = SpringUtils.getBean(SysRoleServiceImpl.class);
//        try {
//            bean.testInner();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        return sysRoleDao.get();
    }

    /**
     * 内部方法！
     *
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
//    @Transactional
    public void testInner() {
        SysRoleEntity entity = new SysRoleEntity();
        entity.setId(1L);
        entity.setDescription("超级管理员11");
        int update = sysRoleDao.update(entity);
        log.info("update: [{}]", update);

        throw new RuntimeException("testInner");
    }
}
